<?php
	//User interface for viewing completed inspections in list format.
	include 'commonAccountFunctions.php';
	include 'templateHandler.php';
	
	//session_start();
	$user = $_SESSION['user'];
	
	$user = checkAuthentication();
	
	print_header();
?>	
		
		<?php 
		$viewFile = isset($_REQUEST["viewFile"]);
		$pdf = isset($_REQUEST["pdf"]);
		$deleteFile = isset($_REQUEST["delete"]);
		$share = isset($_REQUEST["share"]);
		//If the user has selected a file to view.
		if($viewFile){ ?>
			<div class="header">
				<div class="fileInfo">
					<p id="userName">UserName: <?= $user ?> </p>
				</div>
				<form action="index.php" method="post">
					<button type="submit" class="unButtons">Log Out</button>
				</form>
				<form action="controlpanel.php" method="post">
					<button type="submit" class="unButtons">Back</button>
				</form>
			</div>
			<div class="forms"> <?php
			$arrayOfStrings = readcomplete($_REQUEST["file"] . ".txt");
			$parsedTemplate = parseTemplateLines($arrayOfStrings);
			$fileInfo = $parsedTemplate["fileInfo"];
			//print_r($fileInfo);
			loadTemplate($parsedTemplate["groups"], $parsedTemplate["fileInfo"]);
			echo "</div>";
		}
		//the user has selected 
		elseif($deleteFile){
			echo "User clicked Delete";
			if(isset($_REQUEST["file"])){
				$fileName = $_REQUEST["file"];
				unlink("../files/completedInspections/" . $fileName . ".txt"); ?>
				<script type="text/javascript">
				window.location = "completedLists.php";
				</script>
	<?php	}
			else{
				//user did not select a file to delete.
			}
		}
		else{ 
			if(isset($_REQUEST["fac"])){
				$selectedFac = $_REQUEST["fac"];
			}
		
		?>
			
			<div class="header">
				<div class="fileInfo">
					<p id="userName">UserName: <?= $user ?> </p>
				</div>
				<form action="index.php" method="post">
					<button type="submit" class="unButtons">Log Out</button>
				</form>
				<form action="controlpanel.php" method="post">
					<button type="submit" class="unButtons">Back</button>
				</form>
			</div>
			
			<div id="sort">
				<p>Facility:</p>
				<form action="params.php" name="facForm" onchange='changeList()'>
				<select name="fac" value="file">
					<?php $completedList = file("../files/facilityList.txt");
						foreach($completedList as $file){?>
							<option name="room" value="<?=$file?>"> <?=$file?> </option>
						<?php }?>
				</select>
				</form>
			</div>	
			<br />
			
			<script type="text/javascript">
			function changeList(){
				document.facForm.submit();
			}
			</script>
			
			<?php
			//reads in a specified directory that is in the file folder.
				function readFiles($folderName){
					global $selectedFac;
					$dir = "../files/" . $folderName;
					if (is_dir($dir)) {
						if ($dh = opendir($dir)) {
							$i = 0;
							while (($file = readdir($dh)) !== false) {
								if($file != "." and $file != ".."){
									//print_r($file);
									//$lineSplit = explode("_", $file);
									//print_r($lineSplit);
									//echo "$lineSplit[0]";
									//if($lineSplit[0] != $selectedFac){
										//do nothing
									//}
									//else{
										$fileList[$i] = substr($file, 0, -4);
										$i++;
									//}
								}
							}
							closedir($dh);
						}
					}
					return $fileList;
				}
				$completedList = readFiles("completedInspections");
				?>
			<div>
				<p id="completedList">Completed Checklists</p>
				<div id="selections">
					<p>Select |</p>
					<p>File Name</p>
					<form action="completedLists.php">
					<?php foreach($completedList as $file){?>
							<input type="checkbox" name="file[]" value="<?=$file?>"> | <a href="completedLists.php?file=<?=$file?>&viewFile=view"><?=$file?></a> 
							<br />
					<?php }?>
				</div>
				<br />
				<center>
					<button type="submit" class="completedButtons" name="share" value="share" action="params.php">Share Selected</button>
					<button type="submit" class="completedButtons" name="delete" value="delete">Delete Selected</button>
				</center>
				</form>
			</div>
<?php	}

	print_footer();
?>